package net.zhengxinyun.performance.controller.exam;

import net.zhengxinyun.performance.core.Result;
import net.zhengxinyun.performance.core.ResultUtils;
import net.zhengxinyun.performance.entity.ExamIndex;
import net.zhengxinyun.performance.entity.TokenEntity;
import net.zhengxinyun.performance.filter.ExamIndexFilter;
import net.zhengxinyun.performance.service.exam.examClass.ExamIndexService;
import net.zhengxinyun.performance.service.login.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * -- 问卷索引--  问卷标题 生成问卷
 */

@RestController
@RequestMapping("/examIndex")
public class ExamIndexCtrl  {


    @Autowired
    private TokenService tokenService;
    @Autowired
    private ExamIndexService examIndexService;


    @PostMapping("/query")
    public Result<HashMap<String,Object>> query(@RequestBody Param param)
    {

        if(tokenService.compareToken(param.tokenEntity.getValue()))
        {
           HashMap<String,Object> map = new HashMap<>();
            List<ExamIndexFilter> query = examIndexService.query(param.filter);
            if(query.size()==0)
            {
                param.filter.setPageNumber(param.filter.getPageNumber()-1);
                query =examIndexService.query (param.filter);
            }
            int total = examIndexService.queryForCount(param.filter);
            map.put("list",query);
            map.put("pageTotal",total);

            return ResultUtils.getSuccessResults(map);
        }
        return  ResultUtils.getResults(Result.Status.OVERTIME,"error token",null);
    }

    @PostMapping("/insert")
    public  Result<String> insertExamIndex(@RequestBody Param param)
    {
        if(tokenService.compareToken(param.tokenEntity.getValue()))
        {
            int insert = examIndexService.insert(param.entity);
            if(insert ==0){
                return ResultUtils.getResults(Result.Status.ERROR,"操作失败");
            }
            return ResultUtils.getResults(Result.Status.SUCCESS,"操作成功");
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "登录过期");
    }

    @PostMapping("/update")
    public  Result<String> updatetExamIndex(@RequestBody Param param)
    {
        if(tokenService.compareToken(param.tokenEntity.getValue()))
        {
            int insert = examIndexService.updateByPrimaryKey(param.entity);
            if(insert ==0){
                return ResultUtils.getResults(Result.Status.ERROR,"操作失败");
            }
            return ResultUtils.getResults(Result.Status.SUCCESS,"操作成功");
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "登录过期");
    }

    @PostMapping("/delete")
    public  Result<String> deletetExamIndex(@RequestBody Param param)
    {
        if(tokenService.compareToken(param.tokenEntity.getValue()))
        {
            int insert = examIndexService.deleteByPrimaryKey(param.entity.getId());
            if(insert ==0){
                return ResultUtils.getResults(Result.Status.ERROR,"操作失败");
            }
            return ResultUtils.getResults(Result.Status.SUCCESS,"操作成功");
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "登录过期");
    }






}

class Param{

    public ExamIndex entity;
    public ExamIndexFilter filter;
    public TokenEntity tokenEntity;
}